<?php


//------------------------
// 管理后台首页
//-------------------------

namespace app\index\controller;

use app\index\Controller;
use think\Loader;
use think\Session;
use think\Db;
use CFormat;

class Index extends Controller
{
    // 视图类实例
    protected $view;
    // Request实例
    protected $request;
    
    public function __construct() {
        parent::__construct();
        if (null === $this->view) {
            $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
        }
        if (null === $this->request) {
            $this->request = Request::instance();
        }
        // 用户ID
        defined('UID') or define('UID', Session::get(Config::get('rbac.user_auth_key')));
    }

    public function index()
    {
        return $this->view->fetch();
    }
    
    public function shouye(){
        // 查询个人信息
//        $info = Db::name("staff")->where("id", UID)->find();
//        $this->view->assign("info", $info);
        //即将过期
        $time=time();
        $product_1= Db::name('product')
                ->alias('p')
                ->join('product_class pc', 'p.class_id=pc.id', 'LEFT')
                ->join('stock st', 'p.stock_id=st.id', 'LEFT')
                ->field('(p.valid_until-p.warning_day) AS time,p.valid_until,p.name AS pname,pc.name AS cname,p.purchase_price AS pprice,'
                        . 'p.price,st.stock_warehouse AS w_stock,st.stock_shelf AS s_stock,p.valid_until AS until,p.remarks')
                ->where('p.status','<>',0)
                ->having('time<='.$time)
                ->select();
        if($product_1){
            foreach ($product_1 as $_k=>$_v){
                $product_1[$_k]['time']= CFormat::FormatToDay($_v['valid_until']-$time).'天';
//                $product_1[$_k]['until']= CFormat::FormatDate($_v['until'], 'Y-m-d');
            }
        }
        //库存不足
        $product_2= Db::name('product')
                ->alias('p')
                ->join('product_class pc', 'p.class_id=pc.id', 'LEFT')
                ->join('stock st', 'p.stock_id=st.id', 'LEFT')
                ->join('supplier su', 'p.supplier_id=su.id', 'LEFT')
                ->field('(st.stock_warehouse-st.threshold) AS count,p.name AS pname,pc.name AS cname,su.name AS suname,'
                        . 'p.purchase_price AS pprice,su.mobile,su.linkman,'
                        . 'p.price,st.stock_warehouse AS w_stock,st.stock_shelf AS s_stock,p.remarks')
                ->where('p.status','<>',0)
                ->having('count<=0')
                ->select();
//        var_dump($product_2);
        $this->view->assign('overdue', $product_1);
        $this->view->assign('insufficient', $product_2);
        $this->view->assign('sid', UID);
        return $this->view->fetch();
    }
    
    public function head(){
        return $this->view->fetch();
    }
    
    public function sidebar(){
        $role= Db::name('staff')->where('id',UID)->value('role');
        $this->view->role=$role;
        return $this->view->fetch();
    }
}